/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.mvc.controller.explore;

import com.workcell.workdata.im.annotation.CreateAction;
import com.workcell.workdata.im.annotation.UID;
import com.workcell.workdata.im.model.request.NoteAddRequest;
import com.workcell.workdata.im.model.request.NoteUpdateRequest;
import com.workcell.workdata.im.model.response.ResponseEntity;
import com.workcell.workdata.im.model.vo.NoteVO;
import com.workcell.workdata.im.service.NoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/note")
@Api(produces = "application/json", tags = "笔记收藏相关")
public class NoteController {

	@Resource
	private NoteService noteService;

	@ApiOperation(httpMethod = "POST", value = "新增笔记")
	@PostMapping(value = "/add")
	@Validated(CreateAction.class)
	public ResponseEntity<Long> add(@Valid @RequestBody NoteAddRequest request) {
		long id = noteService.add(request);
		return ResponseEntity.ok(id);
	}

	@ApiOperation(httpMethod = "POST", value = "新增笔记")
	@PostMapping(value = "/update")
	@Validated(CreateAction.class)
	public ResponseEntity<Void> update(@Valid @RequestBody NoteUpdateRequest request) {
		noteService.update(request);
		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "DELETE", value = "删除笔记")
	@ApiImplicitParam(name = "id", value = "笔记ID", paramType = "path",  dataTypeClass = Integer.class,example = "0")
	@DeleteMapping(value = "/{id}")
	public ResponseEntity<Void> delete(@PathVariable long id) {
		noteService.delete(id);
		return ResponseEntity.ok();
	}


	@ApiOperation(httpMethod = "GET", value = "获取所有笔记")
	@GetMapping(value = "/list")
	public ResponseEntity<List<NoteVO>> list(@ApiIgnore @UID Long uid) {
		return ResponseEntity.ok(noteService.findList(uid));
	}
}
